﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace WMI.NET
{
    /// <summary>
    /// 
    /// </summary>
    public static class WMI_Conveter
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<byte> GetByteValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return byte.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] GetByteValueArray(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
            {
                List<byte> array = new List<byte>();

                IEnumerable enumerable = mbo[value] as IEnumerable;
                foreach (object e in enumerable)
                {
                    array.Add(byte.Parse( e.ToString()));
                }

                return array.ToArray();
            }
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetStringValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return mbo[value].ToString().Trim();
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string[] GetStringValueArray(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
            {
                List<string> array = new List<string>();

                IEnumerable enumerable = mbo[value] as IEnumerable;
                foreach (object e in enumerable)
                {
                    array.Add(e.ToString());
                }

                return array.ToArray();
            }
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<bool> GetBoolValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return bool.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<uint> GetUIntValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return uint.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<int> GetIntValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return int.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<ulong> GetUlongValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return ulong.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<Double> GetDoubleValue(this ManagementBaseObject mbo , string value)
        {
            if (mbo[value] != null)
                return Double.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ConvertOffset(string value)
        {
            Regex regex = new Regex("(\\d{14}.{7})(.)(\\d{3})");

            string[] DigitGroups = new string[3];

            Match match = regex.Match(value);
            DigitGroups[0] = match.Groups[1].Value;
            DigitGroups[1] = match.Groups[2].Value;
            DigitGroups[2] = match.Groups[3].Value;


            double UTC = Math.Round(double.Parse(DigitGroups[2]) / 60, 2);

            return string.Concat(DigitGroups[0], DigitGroups[1], UTC.ToString("F2").Replace('.', ':'));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <param name="Inputformat"></param>
        /// <returns></returns>
        public static Nullable<DateTimeOffset> GetDateTimeOffsetValue(this ManagementBaseObject mbo, string value, string Inputformat)
        {
            if (mbo[value] != null)
            {
                string convert = ConvertOffset(mbo[value].ToString());
                return DateTimeOffset.ParseExact(convert, Inputformat, CultureInfo.InvariantCulture);
            }

            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<ushort> GetUshortValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return ushort.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Nullable<short> GetShortValue(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
                return short.Parse(mbo[value].ToString());
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static ushort[] GetUshortValueArray(this ManagementBaseObject mbo, string value)
        {
            if (mbo[value] != null)
            {
                List<ushort> array = new List<ushort>();

                IEnumerable enumerable = mbo[value] as IEnumerable;
                foreach (object e in enumerable)
                {
                    array.Add(ushort.Parse(e.ToString()));
                }

                return array.ToArray();
            }
            else
                return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public static Type[] getTypeByName(string className)
        {
            List<Type> returnVal = new List<Type>();

            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type[] assemblyTypes = a.GetTypes();
                for (int j = 0; j < assemblyTypes.Length; j++)
                {
                    if (assemblyTypes[j].Name == className)
                    {
                        returnVal.Add(assemblyTypes[j]);
                    }
                }
            }

            return returnVal.ToArray();
        }

    }
}
